import { EChartsOption, graphic } from 'echarts';

import { DEFAULT_COLOR_OPTIONS, TChartColor } from '@/config/color';
import { getChartListColor } from '@/utils/color';

/** 首页 dashboard 折线图 */
export function constructInitDashboardDataset(type: string) {
  const dateArray: Array<string> = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];

  const datasetAxis = {
    xAxis: {
      type: 'category',
      show: false,
      data: dateArray,
    },
    yAxis: {
      show: false,
      type: 'value',
    },
    grid: {
      top: 0,
      left: 0,
      right: 0,
      bottom: 0,
    },
  };

  if (type === 'line') {
    const lineDataset = {
      ...datasetAxis,
      color: ['#fff'],
      series: [
        {
          data: [150, 230, 224, 218, 135, 147, 260],
          type,
          showSymbol: true,
          symbol: 'circle',
          symbolSize: 0,
          markPoint: {
            data: [
              { type: 'max', name: '最大值' },
              { type: 'min', name: '最小值' },
            ],
          },
          lineStyle: {
            width: 2,
          },
        },
      ],
    };
    return lineDataset;
  }
  const barDataset = {
    ...datasetAxis,
    color: getChartListColor(),
    series: [
      {
        data: [
          100,
          130,
          184,
          218,
          {
            value: 135,
            itemStyle: {
              opacity: 0.2,
            },
          },
          {
            value: 118,
            itemStyle: {
              opacity: 0.2,
            },
          },
          {
            value: 60,
            itemStyle: {
              opacity: 0.2,
            },
          },
        ],
        type,
        barWidth: 9,
      },
    ],
  };
  return barDataset;
}

/**
 *  线性图表数据源
 *
 * @export
 * @param {Array<string>} [dateTime=[]]
 * @returns {*}
 */
export function getLineChartDataSet({
  placeholderColor,
  borderColor,
  xData,
  yData,
}: { xData: any[]; yData: any[] } & TChartColor) {
  const dataSet = {
    color: getChartListColor(),
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'line',
      },
    },
    grid: {
      top: '10%',
      left: 0,
      right: '1%',
      bottom: '10px',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      data: xData,
      boundaryGap: false,
      axisLabel: {
        color: placeholderColor,
      },
      axisLine: {
        lineStyle: {
          width: 1,
          color: borderColor,
          type: 'dashed',
        },
      },
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        color: placeholderColor,
      },
      splitLine: {
        lineStyle: {
          color: borderColor,
          type: 'dashed',
        },
      },
    },
    series: [
      {
        data: yData,
        type: 'line',
        smooth: true,
        areaStyle: {
          color: new graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(51, 102, 255, .3)' },
            { offset: 1, color: 'rgba(255,255,255,0)' },
          ]),
        },
      },
    ],
  };
  return dataSet;
}

/**
 * 获取饼图数据
 *
 * @export
 * @param {array} [data=[]]
 * @returns {*}
 */
export function getPieChartDataSet(data: any[]): EChartsOption {
  return {
    color: DEFAULT_COLOR_OPTIONS,
    tooltip: {
      trigger: 'item',
    },
    grid: {
      top: '10%',
      left: 0,
      right: '3%',
      bottom: 0,
      containLabel: true,
    },
    series: [
      {
        type: 'pie',
        radius: ['58%', '70%'],
        avoidLabelOverlap: false,
        data,
      },
    ],
  };
}
